class Solution
{
public:
    bool isValidSudoku(vector<vector<char>> &board)
    {
        for (int i = 0; i < 9; ++i)
        {
            int rowMask = 0;
            int colMask = 0;
            for (int j = 0; j < 9; ++j)
            {
                if (board[i][j] != '.')
                {
                    int rowBit = 1 << static_cast<int>(board[i][j] - '0');
                    if (rowBit & rowMask)
                    {
                        return false;
                    }
                    else
                    {
                        rowMask |= rowBit;
                    }
                }
                if (board[j][i] != '.')
                {
                    int colBit = 1 << static_cast<int>(board[j][i] - '0');
                    if (colBit & colMask)
                    {
                        return false;
                    }
                    else
                    {
                        colMask |= colBit;
                    }
                }
            }
        }
        for (int i = 0; i < 9; i += 3)
        {
            for (int j = 0; j < 9; j += 3)
            {
                int squareMask = 0;
                for (int x = 0; x < 3; ++x)
                {
                    for (int y = 0; y < 3; ++y)
                    {
                        if (board[i + x][j + y] != '.')
                        {
                            int bit = 1 << static_cast<int>(board[i + x][j + y] - '0');
                            if (squareMask & bit)
                            {
                                return false;
                            }
                            else
                            {
                                squareMask |= bit;
                            }
                        }
                    }
                }
            }
        }
        return true;
    }
};